/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package util.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Rdelacruz
 */
public class TestFileUtil {
    
    public static void main(String[] args) {
        try {
            //test_copiarArchivoADirectorio()
            //test_CrearArchivos();
            test_ExaminarArchivo();
        } catch (IOException ex) {
            Logger.getLogger(TestFileUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private static void test_CrearArchivos() {
        PrintWriter writer;                    
        File f;     
        //Ruta absoluta: desde la raiz de la particion donde esta el proyecto
        escribirArchivo("\\prueba_ruta_absoluta1.txt");//No usar un solo \            
        escribirArchivo("/prueba_ruta_absoluta2.txt");

         //Ruta relativa: desde la carpeta raiz del proyecto
        escribirArchivo("prueba_ruta_relativa1.txt");  
        
        //Equivalentes:
        //escribirArchivo("//192.168.10.150/InfoEnahoXML/prueba.txt");
        //escribirArchivo("\\\\192.168.10.150\\InfoEnahoXML\\prueba.txt");
        
    }
    
    private static void escribirArchivo(String pathname) {
        try {
            PrintWriter writer;
            File f = new File(pathname);
            writer = new PrintWriter(f);
            writer.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    //http://stackoverflow.com/questions/2885173/java-how-to-create-and-write-to-a-file
    private static void test_CrearArchivoPlanoYBinario() {
        PrintWriter writer = null;
        try {
            //Creating a text file (note that this will overwrite the file if it already exists):
            writer = new PrintWriter("the-file-name.txt", "UTF-8");
            writer.println("The first line");
            writer.println("The second line");
            writer.close();
            
            //Creating a binary file (will also overwrite the file):
            byte dataToWrite[] = new byte[]{1,2,3};//...
            FileOutputStream out = new FileOutputStream("the-file-name");
            out.write(dataToWrite);
            out.close();
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            writer.close();
        }
    }

    //http://stackoverflow.com/questions/9235401/java-copy-file-to-another-directory-using-fileutils-and-copyfiletodirectory
    private static void test_copiarArchivoADirectorio() {
        try {
            File destDir = new File("C:\\Folder1\\temp2");
            File srcFile = new File("C:\\Folder1\\temp\\card.png");
            FileUtils.copyFileToDirectory(srcFile, destDir);
        } catch(Exception e) {
        }
    }
    
    private static void test_ExaminarArchivo() throws IOException {        
        File file = new File("D:/TEMP/../TEMP/8126-002.xml");
        examinar(file);        
        
        File file2 = new File("\\\\192.168.10.150\\InfoEnahoXML\\2014","abc.xml");
        examinar(file2);        
        
        File file3 = new File("//192.168.10.150/InfoEnahoXML/2014","abc.xml");
        examinar(file3);
    } 
    
    public static void examinar(File file) throws IOException {
        System.out.println("Path="+file.getPath());
        System.out.println("CanonicalPath="+file.getCanonicalPath());//throws IOException
        System.out.println("AbsolutePath="+file.getAbsolutePath());
                
        System.out.println("Parent="+file.getParent());
        System.out.println("Name="+file.getName());
    }
}
